package com.ufgov.pdf.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;

public class DBHelper {

    /**
     * 将下划线风格替换为驼峰风格
     *
     * @param inputString
     * @return
     */
    private static String underlineToCamelhump(String inputString) {
        StringBuilder sb = new StringBuilder();

        boolean nextUpperCase = false;
        for (int i = 0; i < inputString.length(); i++) {
            char c = inputString.charAt(i);
            if (c == '_') {
                if (sb.length() > 0) {
                    nextUpperCase = true;
                }
            } else {
                if (nextUpperCase) {
                    sb.append(Character.toUpperCase(c));
                    nextUpperCase = false;
                } else {
                    sb.append(Character.toLowerCase(c));
                }
            }
        }
        return sb.toString();
    }
	public static Map<String, Object> getData(String sql) throws ClassNotFoundException, SQLException {
		Class.forName("oracle.jdbc.driver.OracleDriver");
		String url = "jdbc:oracle:thin:@127.0.0.1:1521:orcl";
		String user = "test";
		String password = "1";
		Connection conn = DriverManager.getConnection(url, user, password);
		Statement stmt = conn.createStatement();
		ResultSet rs = stmt.executeQuery(sql);
		ResultSetMetaData data = rs.getMetaData();
		Map<String, Object> map = null;
		while (rs.next()) {
			 map = new HashMap<String, Object>();
			for (int i = 1; i <= data.getColumnCount(); i++) {// 数据库里从 1 开始

				String c = data.getColumnName(i);
				String v = rs.getString(c);
				System.out.println(c + ":" + v + "\t");
				map.put(underlineToCamelhump(c), v);
			}
			System.out.println("======================");
		}
		rs.close();
		stmt.close();
		conn.close();
		return map;
	}
	
	public static void main(String[] args) throws ClassNotFoundException, SQLException {
		String sql  = "select * from cpa_cpaf_bir t where t.report_no ='CG201700042045'";
		System.out.println(getData(sql));
	}
}